package com.sl.transport.mq;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.entity.node.AgencyEntity;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.entity.node.OLTEntity;
import com.sl.transport.entity.node.TLTEntity;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.service.IService;
import com.sl.transport.utils.OrganServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TransportListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = Constants.MQ.Queues.AUTH_TRANSPORT), // 定义队列
            exchange = @Exchange(value = "${rabbitmq.exchange}",type = "topic"), // 定义交换机
            key = "#"
    ))
    public void listenAngencyMsg(String msg){
//        {"type":"ORG",
//            "contnet":[{"managerId":"1","parentId":"0","name":"天津八维营业部","id":"973902113476182273","status":true}],
//            "operation":"UPDATE"}

        log.info(msg);
        JSONObject jsonObject = JSONUtil.parseObj(msg);
        String type = jsonObject.getStr("type");
        if(!StrUtil.equals(type,"ORG")){
            return;
        }

        //获取信息内容
        String operation = jsonObject.getStr("operation");
        JSONObject contentObj = (JSONObject) jsonObject.getJSONArray("content").get(0);

        String name = contentObj.getStr("name");
        Long parentId = contentObj.getLong("parentId");
        Long bid = contentObj.getLong("id");
        Boolean status = contentObj.getBool("status");

        //定义 IService BaseEntity = null
        IService iService;
        BaseEntity baseEntity;
        //根据name判断
        if(StrUtil.endWith(name,"转运中心")){
            //如果是转运中结尾
            //iService = 使用工厂类根据凑得值获取具体实现 OLTService

            //OLTServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.OLT.getCode());
            baseEntity =  new OLTEntity();

            baseEntity.setParentId(0L);

        }else if(StrUtil.endWith(name,"分拣中心")){
            //TLTServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.TLT.getCode());
            baseEntity = new TLTEntity();
            baseEntity.setParentId(parentId);
        } else if (StrUtil.endWith(name,"营业部")) {
            //AgencyServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.AGENCY.getCode());
            baseEntity = new AgencyEntity();
             baseEntity.setParentId(parentId);
        }else {
            log.error("机构名称未按照要求填写 ==> {}",msg);
            return;
        }
        baseEntity.setName(name);
        baseEntity.setBid(bid);
        baseEntity.setStatus(status);
        //根据operation 类型
        switch (operation){
            case "ADD":
                //如果 Add iService.保存
                iService.create(baseEntity);
                break;
            case "UPDATE":
                //如果 UPDATE iService.修改
                iService.update(baseEntity);
                break;
            case "DEl":
                //如果 DEL iService.删除
                iService.deleteByBid(bid);
                break;
        }

    }

}
